package com.heima.wemedia.config;

import com.heima.constants.message.PublishArticleConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 声明定时发布文章
 * 所需的 所有交换机  队列 及 绑定关系
 **/
@Configuration
public class DeclarePubArticleRabbitConfig {
    //延时交换机
    @Bean
    public DirectExchange delayedExchange() {
        return ExchangeBuilder
                .directExchange(PublishArticleConstants.DELAY_DIRECT_EXCHANGE)  //指定交换机类型和名次
                .delayed()  //设置delay属性为true
                .durable(true)  //持久化
                .build();
    }

    //延时队列
    @Bean
    public Queue delayQueue() {
        return new Queue(PublishArticleConstants.PUBLISH_ARTICLE_QUEUE);
    }

    //绑定队列和交换机
    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(delayQueue()).to(delayedExchange()).with(PublishArticleConstants.PUBLISH_ARTICLE_ROUTE_KEY);
    }
}